package com.goods.business.service.imp;

import com.goods.business.mapper.*;
import com.goods.business.pojo.*;
import com.goods.business.service.OutStockService;
import com.goods.common.error.SystemException;
import com.goods.common.model.system.User;
import com.goods.common.model.system.UserRole;
import com.goods.common.vo.business.*;
import com.goods.common.vo.system.UserInfoVO;
import com.goods.system.mapper.UserMapper;
import com.goods.system.mapper.UserRoleMapper;
import com.goods.system.service.UserService;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
@ComponentScan(value = "com.goods.business.mappper")
public class OutStockServiceImpl implements OutStockService {
    @Autowired
    OutStockMapper outStockMapper;
    @Autowired
    OutStockInfoMapper outStockInfoMapper;
    @Autowired
    ProductMapper productMapper;
    @Autowired
    ProductStockMapper productStockMapper;
    @Autowired
    InStockMapper inStockMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    HealthMapper healthMapper;
    @Autowired
    SupplierMapper supplierMapper;
    @Autowired
    ConsumerMapper consumerMapper;
    @Autowired
    UserService userService;

    @Override
    public void OutStockAddOutStock(OutStockVO outStockVO) throws SystemException {
        UserInfoVO info = userService.info();
        OutStock outStock = new OutStock();
        String uuid = UUID.randomUUID().toString();
        outStock.setOutNum(uuid);
        outStock.setOperator(info.getUsername());
        outStock.setCreateTime(new Date());
        outStock.setConsumerId(outStockVO.getConsumerId());
        outStock.setType(outStockVO.getType());
        List<Map<String,Object>> products = (List) outStockVO.getProducts();
        Integer productNumber = 0;
        for (Map<String, Object> product : products) {
            productNumber += (Integer) product.get("productNumber");
            OutStockInfo outStockInfo = new OutStockInfo();
            outStockInfo.setOutNum(uuid);
            outStockInfo.setpNum(productMapper.selectByPrimaryKey(product.get("productId")).getPNum());
            outStockInfo.setProductNumber((Integer) product.get("productNumber"));
            outStockInfo.setCreateTime(new Date());
            outStockInfoMapper.insert(outStockInfo);
        }
        outStock.setProductNumber((Integer)productNumber);
        outStock.setStatus(2);
        outStock.setPriority(outStockVO.getPriority());
        outStock.setRemark(outStockVO.getRemark());
        outStockMapper.insert(outStock);
    }

    @Override
    public List<BizHealth> selhealthHistoryall(String admin, Integer pageNum, Integer pageSize) throws SystemException {
        UserInfoVO info = userService.info();
        User user2 = new User();
        BeanUtils.copyProperties(info, user2);
        User user3 = userMapper.selectOne(user2);
        List<BizHealth> bizHealths = new ArrayList<>();
        List<BizHealth> bizHealths1 = healthMapper.selectAll();
        for (BizHealth health : bizHealths1) {
            if (health.getUserId() == user3.getId()) {
                bizHealths.add(health);
            }
        }
        return bizHealths;
    }

    @Override
    public Date healthReport(String name, BizHealth bizHealth) {
        List<User> users = userMapper.selectAll();
        User user = new User();
        for (User user1 : users) {
            if (user1.getUsername().equals(name)) {
                user = user1;
            }
        }
        bizHealth.setUserId(user.getId());
        Date date = new Date();
        bizHealth.setCreateTime(date);
        healthMapper.insert(bizHealth);
        return date;
    }

    @Override
    public BizHealth health(String name) {
        List<User> users = userMapper.selectAll();
        User user = new User();
        for (User user1 : users) {
            if (user1.getUsername().equals(name)) {
                user = user1;
            }
        }
        // 当前时间
        Date now = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //获取今天的日期
        String nowDay = sf.format(now);
        //对比的时间
//        String day = sf.format(date);
        //return day.equals(nowDay);
//        "yyyy-MM-dd HH:mm:ss"
        List<BizHealth> healthVOS = healthMapper.selectAll();
        DateUtils dateUtils = new DateUtils();
        for (BizHealth healthVO : healthVOS) {
            if (healthVO.getUserId() == (user.getId())) {
                if (dateUtils.isSameDay(healthVO.getCreateTime(), new Date())) {
                    return healthVO;
                }
            }
        }
        return null;
    }

    @Override
    public OutStockDetailVO seloutStockdetailById(Integer pageNum, Integer id) {
        OutStockDetailVO inStockDetailVO = new OutStockDetailVO();
        OutStock outStock = outStockMapper.selectByPrimaryKey(id);
        ConsumerVO consumerVO = consumerMapper.selectByPrimaryKey(outStock.getConsumerId());
        String outNum = outStock.getOutNum();
        List<OutStockInfo> outStockInfos = outStockInfoMapper.selectAll();
        List<ProductVO> productVOS = productMapper.selectAll();
        List<ProductStock> productStocks = productStockMapper.selectAll();
        inStockDetailVO.setConsumerVO(consumerVO);
        List<OutStockItemVO> inStockItemVOS = new ArrayList<>();
        for (OutStockInfo outStockInfo : outStockInfos) {
            if (outStockInfo.getOutNum().equals(outNum)) {
                for (ProductVO productVO : productVOS) {
                    if (productVO.getPNum().equals(outStockInfo.getpNum())) {
                        for (ProductStock productStock : productStocks) {
                            if (productStock.getpNum().equals(productVO.getPNum())){
                                OutStockItemVO inStockItemVO = new OutStockItemVO();
                                inStockItemVO.setCount(productStock.getStock().intValue());
                                BeanUtils.copyProperties(productVO, inStockItemVO);
                                inStockItemVOS.add(inStockItemVO);
                            }
                        }
                    }
                }
            }
        }


        inStockDetailVO.setStatus(2);
        inStockDetailVO.setType(1);
        inStockDetailVO.setOperator("admin");

        inStockDetailVO.setItemVOS(inStockItemVOS);
        return inStockDetailVO;
    }

    @Override
    public List<OutStockVO> findOutStockList(Integer pageNum, Integer pageSize, Integer status, String outNum, Integer type) {
        List<OutStock> outStocks = outStockMapper.selectAll();
        List<OutStockVO> runList = new ArrayList<>();
        if (!StringUtils.isEmpty(status) && StringUtils.isEmpty(outNum) && StringUtils.isEmpty(type)) {
            for (OutStock outStock : outStocks) {
                if (outStock.getStatus().equals(status)) {
                    ConsumerVO consumerVO = consumerMapper.selectByPrimaryKey(outStock.getConsumerId());
                    OutStockVO outStockVO = new OutStockVO();
                    BeanUtils.copyProperties(outStock, outStockVO);
                    outStockVO.setPhone(consumerVO.getPhone());
                    outStockVO.setName(consumerVO.getName());
                    runList.add(outStockVO);
                }
            }
            return runList;
        }

        if (!StringUtils.isEmpty(outNum)) {
            for (OutStock outStock : outStocks) {
                if (outStock.getOutNum().equals(outNum)) {
                    ConsumerVO consumerVO = consumerMapper.selectByPrimaryKey(outStock.getConsumerId());
                    OutStockVO outStockVO = new OutStockVO();
                    BeanUtils.copyProperties(outStock, outStockVO);
                    outStockVO.setPhone(consumerVO.getPhone());
                    outStockVO.setName(consumerVO.getName());
                    runList.add(outStockVO);
                }
            }
            return runList;
        }

        if (!StringUtils.isEmpty(type)) {
            for (OutStock outStock : outStocks) {
                if (outStock.getType().equals(type) && outStock.getStatus().equals(status)) {
                    ConsumerVO consumerVO = consumerMapper.selectByPrimaryKey(outStock.getConsumerId());
                    OutStockVO outStockVO = new OutStockVO();
                    BeanUtils.copyProperties(outStock, outStockVO);
                    outStockVO.setPhone(consumerVO.getPhone());
                    outStockVO.setName(consumerVO.getName());
                    runList.add(outStockVO);
                }
            }
            return runList;
        }

        return runList;
    }
}
